import 'dart:isolate';

///
/// @ProjectName:    flutter_cpp_sdk
/// @Package:        sdk
/// @ClassName:      ClientApp
/// @Description:     描述
/// @Author:         wujehy
/// @Time:     21-2-17 下午7:50
///

import 'package:isolate/ports.dart';
import 'package:ffi/ffi.dart';
import 'dart:convert';
import 'dart:ffi';
import '../ffi/Base.dart';
import '../ffi/ClientMod.dart';

// 这里仅使用 字符串为例
class KeyValueCallbackData extends BaseCallBackData {
  KeyValueCallbackData(int code, String errmsg) : super(code, errmsg);
  String dataStr; // 实际使用可以弄成json 对象
}

/// NOTE wujehy 对上层的封装 , 外面使用的是这些 flutter 封装后的接口
class ClientApp {
  static bool listerner_isinit = false;

  static ReceivePort _lister_port = new ReceivePort();

  /// NOTE wujehy 初始话 原生通道 , 最开始就需要完成这个动作
  /// 这个方法只做一次
  static void initNativeChannel() {
    print("initNativeChannel enter ... ");
    nativeRegisterPostCObject(NativeApi.postCObject);

    _lister_port.listen((message) {
      String lister_type = message[1];
      int event_type_int = message[0];

      DartAppListernerTypeEnum event_type_enum =
          DartAppListernerTypeEnum.values[event_type_int];

      // NOTE wujehy : 这个信息可以 封装到外面去
      print("监听器回包:  类型 ${lister_type}-${event_type_enum}");
    });
    nativeRegisterListernerDartPort(_lister_port.sendPort.nativePort);

    print("initNativeChannel end ... ");
    // 初始化监听器
  }

  /// 客户端初始化的 封装
  /// [status] 状态
  static Future<BaseCallBackData> init([bool status = false]) {
    final future = singleResponseFuture<List>(
        (port) => nativeClientApp_init(port.nativePort, status ? 1 : 0));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      BaseCallBackData callBackData =
          new BaseCallBackData(response[0], response[1] ?? "");
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  /// 反初始化客户端
  static Future<BaseCallBackData> deinit() {
    final future = singleResponseFuture<List>(
        (port) => nativeClientApp_deinit(port.nativePort));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      BaseCallBackData callBackData =
          new BaseCallBackData(response[0], response[1] ?? "");
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  /// 设置缓存
  static Future<BaseCallBackData> setBaseKey(String key, String value) {
    final future = singleResponseFuture<List>((port) =>
        nativeClientApp_setBaseKey(port.nativePort, Utf8.toUtf8(key),
            Utf8.toUtf8(value), value.length));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      BaseCallBackData callBackData =
          new BaseCallBackData(response[0], response[1] ?? "");
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  static Future<BaseCallBackData> delBaseKey(String key) {
    final future = singleResponseFuture<List>((port) =>
        nativeClientApp_delBaseKey(port.nativePort, Utf8.toUtf8(key)));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      BaseCallBackData callBackData =
          new BaseCallBackData(response[0], response[1] ?? "");
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }

  static Future<KeyValueCallbackData> getBaseKey(String key) {
    final future = singleResponseFuture<List>((port) =>
        nativeClientApp_getBaseKey(port.nativePort, Utf8.toUtf8(key)));
    return future.then((List response) {
      print(" callback success respose = $response ");
      //　使用json 解析
      KeyValueCallbackData callBackData =
          new KeyValueCallbackData(response[0], response[1] ?? "");
      String dataStr = utf8.decode(response[2]);
      callBackData.dataStr = dataStr;
      return callBackData;
    }).catchError((err) {
      print(err);
    });
  }
}
